package com.epam.giorgadze.task3.parsers;

import com.epam.giorgadze.task3.AirCompanyVariable;
import com.epam.giorgadze.task3.countries.Country;
import com.epam.giorgadze.task3.entity.AirCompany;
import com.epam.giorgadze.task3.entity.Manufacturer;
import com.epam.giorgadze.task3.entity.PlaneModel;
import com.epam.giorgadze.task3.entity.plane_type.CargoPlane;
import com.epam.giorgadze.task3.entity.plane_type.PassengerPlane;
import com.epam.giorgadze.task3.entity.plane_type.Plane;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * User: Luka
 * Date: 16.03.14
 * Time: 19:24
 */
public class AirCompanyStAXParser {
    AirCompany airCompany;

    public void parse(InputStream inputStream) {
        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        try {
            XMLStreamReader reader = inputFactory.createXMLStreamReader(inputStream);
            process(reader);
        } catch (XMLStreamException e) {

        }
    }

    public AirCompany getAircompany() {
        return airCompany;
    }

    public void process(XMLStreamReader reader) throws XMLStreamException {
        String name = null;
        String text = null;
        Plane plane = null;
        PlaneModel planeModel = null;
        Manufacturer manufacturer = null;
        List<Plane> planes = new ArrayList<Plane>();

        while (reader.hasNext()) {
            int type = reader.next();
            switch (type) {
                case XMLStreamConstants.START_ELEMENT:
                    name = reader.getLocalName();
                    switch (AirCompanyVariable.valueOf(name.toUpperCase())) {
                        case AIRCOMPANY:
                            airCompany = new AirCompany();
                            break;
                        case PLANE:
                            if (reader.getAttributeValue(0).equals("CargoPlane")) {
                                plane = new CargoPlane();
                            }
                            if (reader.getAttributeValue(0).equals("PassengerPlane")) {
                                plane = new PassengerPlane();
                            }
                            break;
                        case PLANEMODEL:
                            planeModel = new PlaneModel();
                            planeModel.setName(reader.getAttributeValue(0));
                            break;
                        case MANUFACTURER:
                            manufacturer = new Manufacturer();
                            break;
                    }
                    break;

                case XMLStreamConstants.END_ELEMENT:
                    name = reader.getLocalName();
                    switch (AirCompanyVariable.valueOf(name.toUpperCase())) {
                        case AIRCOMPANYNAME:
                            airCompany.setAirCompanyName(text);
                            break;
                        case AIRCOMPANYID:
                            airCompany.setAirCompanyID(text);
                            break;
                        case PLANE:
                            planes.add(plane);
                            break;
                        case TRAVELINGDISTANCE:
                            plane.setTravelingDistance(Double.valueOf(text));
                            break;
                        case FUELCONSUMPTION:
                            plane.setFuelConsumption(Double.valueOf(text));
                            break;
                        case PRICE:
                            plane.setPrice(Double.valueOf(text));
                            break;
                        case LIFESPAN:
                            plane.setLifespan(Double.valueOf(text));
                            break;
                        case ENGINECAPACITY:
                            planeModel.setEngineCapacity(Double.valueOf(text));
                            break;
                        case WINGSPAN:
                            planeModel.setWingspan(Double.valueOf(text));
                            break;
                        case PLANELENGTH:
                            planeModel.setPlaneLength(Double.valueOf(text));
                            break;
                        case COURSESPEED:
                            planeModel.setCourseSpeed(Double.valueOf(text));
                            break;
                        case FLIGHTLEVEL:
                            planeModel.setFlightLevel(Double.valueOf(text));
                            break;
                        case COUNTRY:
                            manufacturer.setCountry(Country.valueOf(text));
                            break;
                        case EMAIL:
                            manufacturer.setEmail(text);
                            break;
                        case ADDRESS:
                            manufacturer.setAddress(text);
                            break;
                        case PHONENUMBER:
                            manufacturer.setPhoneNumber(Integer.valueOf(text));
                            break;
                        case NAME:
                            manufacturer.setName(text);
                            break;
                        case TONNAGE:
                            if (plane instanceof CargoPlane) ((CargoPlane) plane).setTonnage(Double.valueOf(text));
                            break;
                        case NUMBEROFSEATS:
                            if (plane instanceof PassengerPlane)
                                ((PassengerPlane) plane).setNumberOfSeats(Integer.valueOf(text));
                            break;
                        case MANUFACTURER:
                            planeModel.setManufacturer(manufacturer);
                            break;
                        case PLANEMODEL:
                            plane.setModel(planeModel);
                    }
                    break;
                case XMLStreamConstants.CHARACTERS:
                    text = reader.getText();
                    break;
            }
        }
        airCompany.setPlanes(planes);
    }

}
